package com.wenhai.liuyubobobo.imooc.sort;

import java.util.Arrays;

/**
 * @ClassName MergeSort
 * @Description 归并排序
 * @Author 谢文海
 * @Date 2021/2/22 19:47
 * @Version 1.0
 **/
public class MergeSort {

    private MergeSort() {

    }
    public static <E extends Comparable<E>> void sort(E[] arr) {
        sort(arr, 0, arr.length - 1);
    }

    private static <E extends Comparable<E>> void sort(E[] arr, int l, int r) {
        if (r - l <= 15) {
            InsertionSort.sort(arr, l, r);
            return;
        }
        int mid = l + ((r - l) >>> 1);
        sort(arr, l, mid);
        sort(arr, mid + 1, r);
        if (arr[mid].compareTo(arr[mid + 1]) > 0) {
            merge(arr, l, mid, r);
        }
    }


    private static <E extends Comparable<E>> void merge(E[] arr, int l, int mid, int r) {
        E[] tempArr = Arrays.copyOfRange(arr, l, r + 1);
        int i = l;
        int j = mid + 1;
        for (int k = l; k <= r; k++) {
            if (i > mid) {
                arr[k] = tempArr[j - l];
                j++;
            } else if (j > r) {
                arr[k] = tempArr[i - l];
                i++;
            } else if (tempArr[i - l].compareTo(tempArr[j - l]) <= 0) {
                arr[k] = tempArr[i - l];
                i++;
            } else {
                arr[k] = tempArr[j - l];
                j++;
            }
        }
    }

    public static <E extends Comparable<E>> void sort2(E[] arr) {
        E[] tempArr = Arrays.copyOf(arr, arr.length);
        sort2(arr, 0, arr.length - 1, tempArr);
    }

    private static <E extends Comparable<E>> void sort2(E[] arr, int l, int r, E[] tempArr) {
        if (r - l <= 15) {
            InsertionSort.sort(arr, l, r);
            return;
        }
        int mid = l + ((r - l) >>> 1);
        sort2(arr, l, mid, tempArr);
        sort2(arr, mid + 1, r, tempArr);
        if (arr[mid].compareTo(arr[mid + 1]) > 0) {
            merge2(arr, l, mid, r, tempArr);
        }
    }



    private static <E extends Comparable<E>> void merge2(E[] arr, int l, int mid, int r, E[] tempArr) {
        System.arraycopy(arr, l, tempArr, l, r - l + 1);
        int i = l;
        int j = mid + 1;
        for (int k = l; k <= r; k++) {
            if (i > mid) {
                arr[k] = tempArr[j];
                j++;
            } else if (j > r) {
                arr[k] = tempArr[i];
                i++;
            } else if (tempArr[i].compareTo(tempArr[j]) <= 0) {
                arr[k] = tempArr[i];
                i++;
            } else {
                arr[k] = tempArr[j];
                j++;
            }
        }
    }


    public static <E extends Comparable<E>> void sortBU(E[] arr) {
        E[] tempArr = Arrays.copyOf(arr, arr.length);
        int n = arr.length;
        for (int i = 1; i < n; i += i) {
            // 合并[j,j+i-1] 和 [j+i, Math.min(j+i+i-1, n-1)]
            for (int j = 0; j + i < n; j += i + i) {
                if (arr[j + i - 1].compareTo(arr[i + j]) > 0) {
                    merge2(arr, j, j + i - 1, Math.min(j + i + i - 1, n - 1), tempArr);
                }
            }
        }

    }


    public static void main(String[] args) {
        Integer[] arr = {5, 4, 6, 7, 8, 3};
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
